<!DOCTYPE html>
<html lang="cn" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>网站设置 Site Setting</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" th:href="@{/statics/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/statics/style/admin.css}" media="all">
    <style type="text/css">
        html{
            background-color: #FFF;
        }
        .pic {
            margin-left: 130px;
        }

        .pic dl {
            float: left;
            position: relative;
        }

        .pic dl dd {
            position: absolute;
            right: 5px;
            top: 5px;
            cursor: pointer;
            background: #666;
            color: #fff;
            padding: 2px;
        }

        .pic img {
            height: 100px;
            margin: 5px 0;
            margin-right: 5px;
        }

    </style>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header">网站设置</div>
        <div class="layui-card-body" style="padding: 15px;">
            <form class="layui-form" action="" lay-filter="layuiadmin-form-site">
                <div class="layui-form-item">
                    <label class="layui-form-label">站点标题</label>
                    <div class="layui-input-block">
                        <input type="text" name="title" lay-verify="required" autocomplete="off" placeholder="请输入站点标题"
                               class="layui-input" th:value="${site.title}">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">站点副标题</label>
                    <div class="layui-input-block">
                        <input type="text" name="subTitle" lay-verify="required" placeholder="请输入站点副标题" autocomplete="off"
                               class="layui-input" th:value="${site.subTitle}">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">站点域名</label>
                    <div class="layui-input-block">
                        <input type="text" name="domain" lay-verify="required" placeholder="请输入邮政编码"
                               autocomplete="off" class="layui-input" th:value="${site.domain}">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">站点LOGO</label>
                    <div class="layui-input-inline">
                        <input type="text" name="logo" id="logo" placeholder="请上传站点LOGO" class="layui-input" th:value="${site.logo}">
                    </div>
                    <button type="button" class="layui-btn upload upload-pic" id="upload-picture" data-des="logo">
                        <i class="layui-icon">&#xe67c;</i>上传站点LOGO
                    </button>
                    <div class="pic pic_box">
                        <dl th:if="${site.logo}">
                            <dt><img th:src="${site.logo}" th:data-url="${site.logo}"></dt>
                            <dd name="deleteFile" th:data-url="${site.logo}" data-des="logo">删除</dd>
                        </dl>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">站点关键字</label>
                    <div class="layui-input-block">
                        <textarea id="keywords" name="keywords" placeholder="请输入站点关键字" class="layui-textarea" style="height: 150px;">[[${site.keywords}]]</textarea>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">站点描述</label>
                    <div class="layui-input-block">
                        <textarea id="description" name="description" placeholder="请输入站点描述" class="layui-textarea" style="height: 150px;">[[${site.description}]]</textarea>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">站点备案</label>
                    <div class="layui-input-block">
                        <input type="text" name="icp" lay-verify="required" placeholder="请输入站点备案" autocomplete="off" class="layui-input" th:value="${site.icp}"/>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">站点模板</label>
                    <div class="layui-input-block">
                        <input type="text" name="theme" lay-verify="required" placeholder="请选择站点模板" autocomplete="off" class="layui-input" th:value="${site.theme}"/>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">统计代码</label>
                    <div class="layui-input-block">
                        <textarea id="statistics" name="statistics" placeholder="请输入电子邮箱" class="layui-textarea" style="height: 150px;">[[${site.statistics}]]</textarea>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">尾部信息</label>
                    <div class="layui-input-block">
                        <textarea id="copyright" name="copyright" placeholder="请输入尾部信息" class="layui-textarea" style="height: 150px;">[[${site.copyright}]]</textarea>
                    </div>
                </div>

                <div class="layui-form-item layui-layout-admin">
                    <div class="layui-input-block">
                        <input type="hidden" name="lang" th:value="${currentlanguage.code}" autocomplete="off"
                               class="layui-input">
                        <button class="layui-btn" lay-submit="" id="LAY-site-form-submit" lay-filter="LAY-site-form-submit">
                            立即提交
                        </button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>


<script th:src="@{/statics/layui/layui.js}"></script>
<script>
    layui.config({
        base: '/statics/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form', 'upload'], function () {
        var $ = layui.$
            , upload = layui.upload
            , form = layui.form;

        form.render(null, 'layuiadmin-form-site');

        upload.render({
            elem: '.upload-pic'
            , url: '/api/file/local/upload'
            , accept: 'images'
            , method: 'post'
            , multiple: false //多文件上传
            , acceptMime: 'image/*'
            ,before: function(obj){
                layer.load(); //上传loading
            }
            ,done: function(res){
                var item = this.item;
                var des=$(item).data('des');
                console.log(des);
                layer.closeAll('loading'); //关闭loading
                if(res.code==0){
                    $('#'+des).val(res.data);
                    layer.msg('上传成功！');
                }else{
                    layer.msg('上传失败！');
                }
            }
            ,error: function(){
                layer.closeAll('loading'); //关闭loading
                layer.msg('上传发生错误！');
            }
        });

        /* 监听提交 */
        form.on('submit(LAY-site-form-submit)', function (data) {
            $.ajax({
                url: '/api/site/update',
                method: 'post',
                data: data.field,
                dataType: 'JSON',
                async: true,
                success: function (result) {
                    if (result.code == 0) {
                        layer.msg('保存成功!', {icon: 1, time: 1000});
                    } else {
                        layer.msg('保存失败！' + result.msg, {icon: 2, time: 1000});
                    }
                },
                error: function (err) {
                    layer.msg('保存失败！', {icon: 2, time: 1000});
                }
            });
            return false;
        });

        $('dd[name="deleteFile"]').on('click' , function(){
            var url = $(this).data('url');
            var des = $(this).data('des');
            var picbox = $(this).parent().parent();
            $.ajax({
                url: '/api/file/local/remove?key=' + url,
                method: 'DELETE',
                contentType: "application/json;charset=UTF-8",
                dataType: 'JSON',
                success: function (res) {
                    if (res.code == '0') {
                        layer.msg('文件已删除!', {icon: 1, time: 1000}, function () {
                            picbox.hide();
                            $('#'+des).val('');
                        });
                    } else {
                        layer.msg(res.msg);
                    }
                },
                error: function (err) {
                }
            });
        });

    });
</script>

</body>
</html>
